﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using Tauren.Form.DLL.Form_TableInfoDLL;
using Tauren.Form.DLL.Resouces;
using Tauren.Form.Entity.Config;
using Tauren.Form.Entity.Const;
using Tauren.Form.Entity.Enum;
using Tauren.Form.Entity.Global;
using Tauren.Form.Entity.Input;
using Tauren.Form.Entity.Model;
using Tauren.Form.Entity.Output;
using Tauren.Form.Infrastructure.Common;
using Tauren.Form.Infrastructure.Globals;

namespace Tauren.Form.Service.Form_TableInfoBLL
{
    public class V2Form_TableInfoService : Form_TableImImpService
    {
        public override double VersionName => 2.0;
        private IForm_TableInfoBusiness business;
        #region public
        public V2Form_TableInfoService(IForm_TableInfoBusiness _business, IConnectionBLLBase connectionBLL, IMenuResourcesBusiness resourcesBusiness) : base(_business, connectionBLL, resourcesBusiness)
        {
            business = _business;
            this.ResourcesInfo = this.GetResources(GlobalConst.RESOURCES_FLAG_MODULE_FORM_TABLEINFO_KEY);
            this.JudePrmission(GlobalConst.RESOURCES_FLAG_MODULE_FORM_TABLEINFO_KEY.ToLower());
        }
        public override IResponseMessage Pages(QueryModel queryModel)
        {
            if (!HavePermission) return ErrorType.BUSINESS_WITHOUT_PERMISSION.Failure(this.CommonText(ErrorType.BUSINESS_WITHOUT_PERMISSION));

            ///获取基础数据库中表信息
            List<TableInfo> tableInfos = business.GetAll<TableInfo>();
            TableListModel<PageForm_TableInfoTable> tableListModel = new TableListModel<PageForm_TableInfoTable>();
            List<TableListHeaderModele> Headers = new List<TableListHeaderModele>();
            Headers.Add(new TableListHeaderModele() { Key = "rowNo", Name = CommonText("rowNo"), Width = 50, Align = "center", Fixed = true });
            Headers.Add(new TableListHeaderModele() { Key = "autoId", Name = CommonText("autoid"), Width = 70, Hide = true, Type = "primary_key", Align = "center", SorTable = SortType.custom.ToString(), Fixed = true });
            Headers.Add(new TableListHeaderModele() { Key = "tableCode", Name = CommonText("thcode"), Width = 160, Align = "left", Type = "primary_key", SorTable = SortType.custom.ToString(), Fixed = true, Filter = true });
            Headers.Add(new TableListHeaderModele() { Key = "cnName", Name = CommonText("thcnname"), Width = 220, Align = "left", IsOperation = true, Fixed = true, Filter = true });
            Headers.Add(new TableListHeaderModele() { Key = "enName", Name = CommonText("thenname"), Width = 220, Align = "left", Fixed = true, Filter = true });
            Headers.Add(new TableListHeaderModele() { Key = "foreignKey", Name = CommonText("fieldcode"), Width = 160, Align = "left", SorTable = SortType.custom.ToString() });
            Headers.Add(new TableListHeaderModele() { Key = "fieldName", Name = CommonText("thcnname"), Width = 220, Align = "left" });
            Headers.Add(new TableListHeaderModele() { Key = "fieldENName", Name = CommonText("thenname"), Width = 220, Align = "left" });
            Headers.Add(new TableListHeaderModele() { Key = "dic_GroupType", Name = Resources("thgrouptype"), Width = 120, Align = "center", IsTag = true, });
            Headers.Add(new TableListHeaderModele() { Key = "dic_Records", Name = Resources("threcords"), Width = 120, Align = "center", IsTag = true });
            Headers.Add(new TableListHeaderModele() { Key = "dic_Enable", Name = CommonText("thstate"), IsTag = true, Width = 100, Align = "center", SorTable = SortType.custom.ToString() });
            Headers.Add(new TableListHeaderModele() { Key = "dic_Creator", Name = CommonText("diccreator"), Width = 120, Align = "center" });
            Headers.Add(new TableListHeaderModele() { Key = "dic_Creatdate", Name = CommonText("diccreatdate"), Width = 160, Align = "center" });

            Headers.Add(new TableListHeaderModele() { Key = "action", Name = "操作", Width = 180, Align = "center" });

            long Total = 0;
            List<PageForm_TableInfoTable> Data = business.Pages(queryModel, out Total);
            List<PageForm_TableInfoTable> res = Data.Join(tableInfos, a => a.TableCode.ToLower(), b => b.TableCode.ToLower(), (a, b) =>
            {
                a.GroupType = b.GroupType;
                a.Records = b.Records;
                a.CNName = b.CNName;
                a.ENName = b.ENName;
                return a;
            }).ToList();

            if (res.Any())
            {
                res.ForEach(o =>
                {
                    o.Dic_Records = o.Records.Value ? CommonText("globalyes") : CommonText("globalno");
                    o.Dic_GroupType = this.CommonText($"dictionary{(DictionaryType)o.GroupType}");
                    o.Dic_Enable = CommonText(o.Enable ? GlobalConst.RESOURCES_CONTENT_ENABLE_KEY : GlobalConst.RESOURCES_CONTENT_DISABLE_KEY);
                    o.Dic_Creator = EmpDictionary.ContainsKey(o.Creator) ? EmpDictionary[o.Creator] : "";
                    o.Dic_Creatdate = o.Creatdate.Value.ToString(GlobalConst.DATE_FORMAT_SECONDS);
                    o.Row_Style = o.Enable ? "" : "danger";
                    SwitchFieldKey(o);
                    o.Operations = new List<SystemButton>();
                    if (Global.IsAdmin && (Buttons is null || !Buttons.Any()))
                    {
                        BuliderButtons(o.Operations, o.TableCode, o.AutoId, ButtomType.edit);
                        BuliderButtons(o.Operations, o.TableCode, o.AutoId, ButtomType.detaile);
                        BuliderButtons(o.Operations, o.TableCode, o.AutoId, !o.Enable ? ButtomType.enable : ButtomType.disable);
                        BuliderButtons(o.Operations, o.TableCode, o.AutoId, ButtomType.delete);
                    }
                    else
                    {
                        if (Buttons.Any(o => o.Location == ButtomLocation.center.ToString() && o.Value == ButtomType.edit.ToString()))
                            BuliderButtons(o.Operations, o.TableCode, o.AutoId, ButtomType.edit);
                        if (Buttons.Any(o => o.Location == ButtomLocation.center.ToString() && o.Value == ButtomType.detaile.ToString()))
                            BuliderButtons(o.Operations, o.TableCode, o.AutoId, ButtomType.detaile);
                        if (Buttons.Any(o => o.Location == ButtomLocation.center.ToString() && (o.Value == ButtomType.enable.ToString() || o.Value == ButtomType.disable.ToString())))
                            BuliderButtons(o.Operations, o.TableCode, o.AutoId, !o.Enable ? ButtomType.enable : ButtomType.disable);
                        if (Buttons.Any(o => o.Location == ButtomLocation.center.ToString() && o.Value == ButtomType.delete.ToString()))
                            BuliderButtons(o.Operations, o.TableCode, o.AutoId, ButtomType.delete);
                    }
                });
            }
            tableListModel.Body = res;
            tableListModel.Total = Total;
            tableListModel.Header = Headers;
            if (Global.IsAdmin && (Buttons is null || !Buttons.Any()))
            {
                BuliderButtons(tableListModel.Operations, "", 0, ButtomType.add);
            }
            else
            {
                if (Buttons.Any(o => o.Value == ButtomType.add.ToString()))
                    BuliderButtons(tableListModel.Operations, "", 0, ButtomType.add);
            }
            return tableListModel.Succeed();
        }
        #endregion 
    }
}
